jQuery(document).ready(function(){
    jQuery("#submit").click(function(){
        log=check_logic();
        log_text="";
        for(var i=0;i<log.length;i++)
            {
            if(log[i]!=true)
                log_text+=log[i]+"<br>";
            else
                {
                if(i==log.length-1)
                    {
                    log_text+="<br>All test successfull!"; 
                }
            }
        }
        jQuery("#test_result").html(log_text);
        if(log[log.length-1])
            {
            //submit
            jQuery.ajax({
                type: "POST",
                url: "index.php",
                data: { controller: "quizzes", action: "post_string",ajax_action:1,default_type:jQuery("#default_type").val(),description:jQuery("#description").val(),name:jQuery("#name").val(),collect_content: jQuery("#editor").html(),quiz_id:_quiz_id}
            })
            .done(function( msg ) {
                alert( "Data Saved: " + msg );
                jQuery("#editor").html("");
            });
        }
    });

});
function check_logic()
{
    str=jQuery("#editor").html();
    logs=[];
    str = replaceAll("\r\n",'',str);
    str = replaceAll("<div>",'<br>',str);
    str = replaceAll("<hr>",'',str);
    if(str.length>100000)
        {
        logs.push("Failure, max char is 100.000");
        return logs;
    }        
    quizzes_str = str.split(quiz_new);
    if(quizzes_str.length>100)
        {                
        logs.push("Failure, max quizzes is 100");
        return logs;
    }
    for(var i=0;i<quizzes_str.length;i++)
        {
        logs.push("<hr>Quiz "+i+" begin");
        current="";
        current=quizzes_str[i];
        if(current.length<quiz_min_length)
            {
            logs.push("Quiz "+i+" required min length is "+quiz_min_length+", skip!");
            continue;
        }

        quiz_strs=current.split(quiz_ans);
        for(var j=0;j<quiz_strs.length;j++)
            {
            if((quiz_strs[j].trim()).length<4)
                {
                logs.push("Quiz "+i+" skip!");
                continue;
            }

            if(j==0)
                {

                var re = new RegExp("_p%([0-9]+)");
                _point=quiz_strs[j].match(re);                        
                if(_point==null)
                    {
                    logs.push("Quiz "+i+" set default point is 0");
                }
                var re1 = new RegExp("_d%([0-9]+)");
                _point1=quiz_strs[j].match(re1);                        
                if(_point1==null)
                    {
                    logs.push("Quiz "+i+" set default diff is "+quiz_default_diff);
                }
                //----

                continue;
            }
            if(quiz_strs[j].indexOf(quiz_right)>0)
                {
                logs.push("Right ans is "+replaceAll(quiz_right,'',quiz_strs[j]));

            }

        }

    }
    logs.push(true);
    return logs;

    //test_result
}
function escapeRegExp(str) {
    return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}
function replaceAll(find, replace, str) {
    return str.replace(new RegExp(escapeRegExp(find), 'g'), replace);
}
function escapeRegExp(str) {
    return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}